/**
 * Created by maxiaoqiang on 2018/7/12.
 */
import React, {Component} from "react";

import {Table, Button, message} from 'antd';

import ProducePaperHeader from "component/header/ProducePaperHeader";

import moment from "moment";
import {HttpClient} from "util/HttpClient";
import ExamContent from "./ExamContent";
import {local} from "util/MyUtil";

import "./index.less";

const typeData = {
    0:"简单",
    1:"一般",
    2:"难",
    3:"困难"
};

class Official extends Component{

    constructor(props){
        super(props);
        this.state = {
            courseId:"",
            visible: false,
            dataSource: []
        };
    }

    componentDidMount(){
        this.initPaper();
    }

    initPaper=()=>{
        HttpClient.get("/v1/paper/list?userId="+local.get("userInfo").id)
            .then((response)=>{
                if(response.data.code==200){
                    let daraSource = [];
                    let datas = response.data.data;
                    for(var i in datas){
                        let data = datas[i].paper;
                        data.key = data.id;
                        daraSource.push(data);
                    }
                    this.setState({
                        dataSource:daraSource
                    })
                }
            });
    };

    getColumns =()=>{
        return(
            [
                {
                    title: '序号',
                    dataIndex: 'id',
                    align: "center"
                }, {
                    title: '试卷名称',
                    dataIndex: 'name',
                    align: "center"
                }, {
                    title: '难度等级',
                    dataIndex: 'level',
                    align: "center",
                    render: (text, record) => {
                        return (
                            <div>
                                {typeData[record.level]}
                            </div>
                        )
                    }
                }, {
                    title: '及格线',
                    dataIndex: 'passValue',
                    align: "center",
                    render: (text, record) => {
                        return (
                            <div>
                                {record.passValue}
                            </div>
                        )
                    }
                }, {
                    title: '考试时间',
                    dataIndex: 'start_time',
                    align: "center",
                    render: (text, record) => {
                        return (
                            <div>
                                {moment(parseInt(record.startTime*1000)).format("YYYY-MM-DD HH:mm:ss")}<br/>
                                {moment(parseInt(record.endTime)*1000).format("YYYY-MM-DD HH:mm:ss")}
                            </div>
                        )
                    }
                }, {
                    title: '操作',
                    dataIndex: 'operation',
                    align: "center",
                    render: (text, record) => {
                        return (
                            <div>
                                {record.endTime > new Date().getTime()/1000?
                                    <Button
                                        key={"paper_"+record.id}
                                        type="primary"
                                        onClick={()=>{
                                            this.beginExamHandle(record.id, record)
                                        }}
                                    >
                                        开始考试
                                    </Button>
                                    :
                                    <div className="exam-over">
                                        --结束--
                                    </div>
                                }
                            </div>
                        )
                    }
                }
            ]
        )
    };

    beginExamHandle=(paperId, data)=>{
        HttpClient.get("/v1/paper/begin?paperId="+paperId)
            .then((response)=>{
                if(response.data.code==200){
                    this.setState({
                        visible:true,
                        examData:data
                    })
                }else{
                    message.error(response.data.msg)
                }
            })
    }

    handleCommit=(paperId, list)=>{
        HttpClient.post("/v1/history/commit/"+paperId, list)
            .then((response)=>{
                console.log(response.data);
                if(response.data.code==200){
                    message.success("交卷成功");
                    this.setState({
                        visible:false
                    })
                }
            });
    };

    createPaperHandle=(data)=>{
        HttpClient.post("/v1/paper/create/"+local.get("userInfo").id, data)
            .then((response)=>{
                if(response.data.code==200){
                    message.success("试卷生成成功");
                    this.initPaper();
                }
            });
    };

    render(){
        return(
            <div>
                {this.state.visible?
                    <ExamContent
                        data={this.state.examData}
                        commitPaper={this.handleCommit}
                    />
                    :
                    null
                }
                <ProducePaperHeader
                    createPaper={this.createPaperHandle}
                />
                <div>
                    <Table
                        bordered
                        dataSource={this.state.dataSource}
                        columns={this.getColumns()}
                    />
                </div>
            </div>
        )
    }
}


export default Official;